class PromotionMailsController < ApplicationController
  skip_before_action :check_permission!
  before_action :check_permission_for_creation!, only: [:new, :create, :edit, :update, :deliver_test]
  before_action :check_permission_for_delivery!, only: [:deliver, :destroy]
  before_action :set_promotion_mail, only: [:show, :edit, :update, :destroy, :deliver, :do_deliver, :deliver_test, :do_deliver_test, :view, :pause, :resume, :stop]

  # GET /promotion_mails
  # GET /promotion_mails.json
  def index
    @status = params[:status]
    @promotion_mails = PromotionMail.where(deleted: false).order(id: :desc)
    @promotion_mails = @promotion_mails.order(updated_at: :desc).page(params[:page]).per(10)
  end

  # GET /promotion_mails/1
  # GET /promotion_mails/1.json
  def show
  end

  # GET /promotion_mails/new
  def new
    @promotion_mail = PromotionMail.new
  end

  # GET /promotion_mails/1/edit
  def edit
  end

  # POST /promotion_mails
  # POST /promotion_mails.json
  def create
    @promotion_mail = PromotionMail.new(promotion_mail_params.merge(created_by: current_admin))

    respond_to do |format|
      if @promotion_mail.save_with_macro_compilation
        format.html { redirect_to @promotion_mail, notice: 'Promotion mail was successfully created.' }
        format.json { render action: 'show', status: :created, location: @promotion_mail }
      else
        format.html { render action: 'new' }
        format.json { render json: @promotion_mail.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /promotion_mails/1
  # PATCH/PUT /promotion_mails/1.json
  def update
    respond_to do |format|
      if @promotion_mail.update_with_macro_compilation(promotion_mail_params)
        format.html { redirect_to @promotion_mail, notice: 'Promotion mail was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @promotion_mail.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /promotion_mails/1
  # DELETE /promotion_mails/1.json
  def destroy
    @promotion_mail.update(deleted: true)
    respond_to do |format|
      format.html { redirect_to promotion_mails_url }
      format.json { head :no_content }
    end
  end

  def deliver
    @user_groups = UserGroup.all.order(name: :asc)
    @users = User.none
  end

  def pause
    @promotion_mail.deliveries.each(&:pause)
    render text: promotion_mails_path
  end

  def resume
    @promotion_mail.deliveries.each(&:resume)
    render text: promotion_mails_path
  end

  def stop
    @promotion_mail.deliveries.each(&:stop)
    render text: promotion_mails_path
  end

  def do_deliver
    user_ids = params[:user_ids] || []
    user_group_ids = params[:user_group_ids]
    recipients = User.where('id in (?) or user_group_id in (?)', user_ids, user_group_ids)
    Deliverer.new(@promotion_mail, recipients, current_admin).deliver
    redirect_to @promotion_mail, notice: 'Delivery started.'
  end

  def deliver_test
    # TODO
  end

  def do_deliver_test
    # TODO
  end

  def view
    @promotion_mail.html_part.gsub!('$BACKGROUND', "/assets/mails/#{@promotion_mail.id}/background.jpg")
    render layout: false
  end

  private
    def check_permission_for_creation!
      not_found unless current_admin.can? :create_mail
    end

    def check_permission_for_delivery!
      not_found unless current_admin.can? :deliver_mail
    end

    # Use callbacks to share common setup or constraints between actions.
    def set_promotion_mail
      @promotion_mail = PromotionMail.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def promotion_mail_params
      params.require(:promotion_mail).permit(:subject, :text_part, :html_part, :status, :attachment, :draft)
    end
end
